package me.youline.dataServer.service.authentication;

import java.util.List;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * shiro securityManager的realms初始化业务类
 * 在集成shiro过程中，我们发现自定义的realms由于和系统的其他业务类有关联，
 * 在shirofilter创建时候会导致自定义realms过早的被初始化，这些自定义realms存在的transaction标签此时都会失效。 
 * 因为他们会优先于事务的advisor初始化，因此我们将初始化工作移至到容器执行refresh事件发布的时候执行。就可以避免事务缺失的问题
 * @author linhan
 *
 */
public class SecurityManagerRealmInitialService implements ApplicationListener<ContextRefreshedEvent>{
	
	private SecurityManager securityManager;
	
	private List<Realm> realms;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		DefaultWebSecurityManager webSecurityManager = (DefaultWebSecurityManager) securityManager;
		if(realms != null && ! realms.isEmpty()){
			webSecurityManager.setRealms(realms);
		}
	}

	public List<Realm> getRealms() {
		return realms;
	}

	public void setRealms(List<Realm> realms) {
		this.realms = realms;
	}

	public SecurityManager getSecurityManager() {
		return securityManager;
	}

	public void setSecurityManager(SecurityManager securityManager) {
		this.securityManager = securityManager;
	}
	
}
